/*
 * @Description: 
 * @Author: xubin
 * @Date: 2022-03-03 16:27:52
 * @LastEditors: xubin
 * @LastEditTime: 2022-05-11 14:50:18
 */
import __DB__               from '../db/index.js';
import __canvas__           from '../canvas/index.js';
import __node__             from '../node/index.js';
import __tool__             from '../tool/index.js';
import __customEvent__      from '../customEvent/index.js';

class keycodeClass{
    constructor(){
        
    }

    init(){
        let canvas = document.getElementById('xCanvas');
        canvas.focus();
        canvas.onkeydown = (e)=>{
            let code = e.keyCode;
            if(code === 17){
                this.ctrl('down');
            }
        }

        canvas.onkeyup = (e)=>{
            let code = e.keyCode;
            if(code === 17){
                this.ctrl('up');
            }
            
            if(code === 46){
                this.delete();
            }

            if(code === 13){
                this.enter();
            }
            // console.log(code)
        }
        
    }

    ctrl(type){
        __DB__.nodeCheckbox.is = type==='down'?true:false;
    }

    delete(){
        let nodeCheck       = __DB__.nodeCheck;
        let nodeCheckbox    = __DB__.nodeCheckbox.items;
        let nodeCheckbox_is = __DB__.nodeCheckbox.is;

        __customEvent__.set({
            type:"x_keyup",
            data:{
                name:'del',
                id:nodeCheck,
            }
        });

        return;

        if(nodeCheckbox_is){
            if(nodeCheckbox.length > 0){
                nodeCheckbox.forEach(el=>{
                    __DB__.del(el);
                });
                __DB__.nodeCheckbox.items = [];
                __DB__.reset();
            }
        }

        if(nodeCheck){
            __DB__.del(nodeCheck);
            __DB__.nodeCheck = '';
            __DB__.reset();
        }
        
    }

    enter(){
        let nodeCheck = __DB__.nodeCheck;
        let nodeCheckbox = __DB__.nodeCheckbox.items;
        let nodecs = [];
        if(nodeCheckbox.length > 0){
            nodecs = nodeCheckbox;
        }
        else{
            nodecs = [nodeCheck];
        }

        __customEvent__.set({
            type:"x_keyup",
            data:{
                name:'enter',
                nodes:nodecs,
            }
        });
    }
}

let keycode = new keycodeClass();
export default keycode;